<template>
  <div id="popup" v-if="visible">
    <!-- 弹窗遮蔽层 -->
    <div class="dialog-face" @click="closePopup">
    </div>
    <!-- 弹窗内容层 -->
    <div class="bg">
      <div class="title">
        好友广场
        <div class="close" @click="closePopup()">&times;</div>
      </div>
      <div class="search">
        <input class="search_input" @keyup.enter="searchUser" placeholder="昵称/用户名" v-model="searchName" type="text">
        <button class="search_btn" @click="searchUser">搜索</button>
      </div>
      <div class="user_result">
        <div class="result_title">
          <span v-if="searchName">搜索结果</span>
          <span v-else>每日推荐</span>
        </div>
        <div v-for="item in userList.value" class="search_user_box">
          <div class="avatar_box">
            <img class="avatar" :src="item.avatar" alt="">
          </div>
          <div class="user_info_box">
            <span class="name">{{ item.name }}</span>
            <span class="user_name">{{ item.userName }}</span>
          </div>
          <span v-if="item.friendId" class="added">已添加</span>
          <span v-else-if="!item.friendId && item.applyId && item.userId===user.id" class="applying">
            申请中
          </span>
          <span v-else-if="!item.friendId && item.userId && item.applyId===user.id" class="wait_handle">
            待接受
          </span>
          <el-popover popper-class="apply" :visible="item.popoverVisible" v-else placement="bottom" :width="400" trigger="click">
            <template #reference>
              <el-button class="add_btn" @click="clear(item)">添加</el-button>
            </template>
            <div class="apply_box">
              <div class="search">
                <input class="search_input" @keyup.enter="apply(item.id)" placeholder="验证信息" v-model="verifyText" type="text">
                <button class="search_btn" @click="apply(item.id)">申请</button>
                <span @click="closePopover(item)">×</span>
              </div>
            </div>
          </el-popover>

        </div>

      </div>
    </div>
  </div>
</template>

<script setup>
// 打开弹窗

import {onMounted, reactive, ref, watch} from "vue";
import {applyUser, searchUserList} from "@/api/user/index.js";
import {notice} from "@/util/playMsgNitific.js";
import {getUser} from "@/api/login/index.js";
import socketSend from "@/util/socket.js";

let props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  name:{
    type:String,
    default:null
  }
});
let popoverVisible = ref(false)
let verifyText = ref('')
let user = ref(null)

watch(()=>props.visible,(value)=>{
  if (value){
    searchUser()
  }
})

watch(()=>props.name,(value)=>{
  if (value){
    searchName.value = value
  }
})

function apply(id){
  popoverVisible.value = false
  applyUser({'applyUserId':id,msg:verifyText.value}).then(res=>{
    notice('success','申请成功','好友申请')
    searchUser()
  })
}

function closePopover(item){
  item.popoverVisible = false
}

function clear(item){
  item.popoverVisible = true
  verifyText.value = null
}
function searchUser(){
  searchUserList({name:searchName.value}).then(res=>{
    userList.value = res.data.data
  })
}

let userList = reactive([])

let searchName = ref('')

let emit = defineEmits(['close'])

onMounted(()=>{
  getUser().then(res=>{
    user.value = res.data.data
  })
})
// 关闭弹窗
function closePopup() {
  emit('close')
}
</script>

<style lang="less" scoped>
/* 遮挡层样式 */
.dialog-face {
  position: fixed;
  height: 100%;
  width: 100%;
  z-index: 10;
  top: 0;
  left: 0;
  opacity: 0.5;
  background: #000000;
}

/* 弹窗样式 */
.bg {
  width: 320px;
  height: 424px;
  background-color: white;
  border-radius: 5px;
  box-shadow: 0px 3px 8px 0px rgba(0, 0, 0, 0.2);
  padding: 20px;
  /* 定位 居中 */
  position: absolute;
  left: 50%;
  top: 50%;
  margin-top: -170px;
  margin-left: -200px;
  /* 显示级别 */
  z-index: 11;
}

/* 弹窗标题 */
.title {
  color: #000000;
  text-align: start;
  height: 20px;
  line-height: 13px;
  cursor: move;
  /*光标的样式*/
  padding: 0;
  font-size: 15px;
  border-bottom: 1px solid rgba(236, 236, 236, 1);
}
.apply{
  position: relative;
  span{
    top: 0;
    right: 0;
    font-size: 25px;
    line-height: 20px;
    position: absolute;
    cursor: pointer;
  }
}
.search{
  width: 100%;
  margin: 10px 0;
  text-align: center;
  display: flex;


  .search_input{
    width: 65%;
    border-bottom-left-radius: 5px;
    border-top-left-radius: 5px;
    padding: 0 15px;
    height: 29px;
    border: none;
    background-color: #f5f5f5;
  }
  .search_input:focus{
    outline-style: none;
  }

  .search_btn{
    border: none;
    cursor: pointer;
    width: 30%;
    border-radius: 0 5px 5px 0;
    padding: 2px;
    color: white;
    background-color: #07c160;
  }
  .search_btn:hover {
    border: 1px solid #d2d2d2;
    background-color: #d2d2d2;
  }

  .search_btn:focus {
    outline: none;
  }

  .search_btn:active {
    background-color: #c6c6c6;
  }
}

.user_result{
  height: 355px;
  overflow-y: scroll;
  .result_title{
    font-size: 13px;
    text-align: start;
    color: #999;
  }
}
.search_user_box{
  position: relative;
  display: flex;
  width: 100%;
  height: 70px;
  .avatar_box{
    padding: 10px;
    .avatar{
      height: 100%;
      border-radius: 5px;
    }
  }
  .user_info_box{
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    justify-content: space-around;
    .user_name{
      font-size: 13px;
      color: #7f7f7f;
    }
  }
  .add_btn{
    position: absolute;
    padding: 1px 15px;
    right: 20px;
    bottom: 15px;
    border-radius: 5px;
    color: black;
    font-weight: 400;
    background-color: white;
    height: 25px;
    letter-spacing:3px;
    border: 1px solid #c4c4c4;
  }
  .add_btn:hover{
    background-color: #f5f5f5;
  }
  .add_btn:focus{
    outline-style: none;
  }
  .added{
    position: absolute;
    right: 20px;
    bottom: 15px;
    color: #07c160;
  }
  .applying{
    position: absolute;
    right: 20px;
    bottom: 15px;
    color: #eaa364;
  }
  .wait_handle{
    position: absolute;
    right: 20px;
    bottom: 15px;
    color: #55b3ee;
  }
}
/* 弹窗上的关闭元素样式 */
.close {
  position: absolute;
  right: 6px;
  top: 16px;
  width: 30px;
  height: 30px;
  cursor: pointer;
  text-decoration: none;
  font-weight: 300;
  font-size: 30px;
  color: #999999;
}

.close:hover {
  color: #fa6965;
}
</style>